import { defineStore } from 'pinia'
import { getMenuTreeNoButton } from '@/api/menu'

export const useMenuStore = defineStore('menu', {
  state: () => ({
    menuTree: null
  }),
  actions: {
    async fetchMenuTree() {
      if (this.menuTree) return this.menuTree
      const local = localStorage.getItem('menuTree')
      if (local) {
        this.menuTree = JSON.parse(local)
        return this.menuTree
      }
      const res = await getMenuTreeNoButton()
      this.menuTree = res.data.data || []
      localStorage.setItem('menuTree', JSON.stringify(this.menuTree))
      return this.menuTree
    },
    setMenuTree(tree) {
      this.menuTree = tree
      localStorage.setItem('menuTree', JSON.stringify(tree))
    },
    clearMenu() {
      this.menuTree = null
      localStorage.removeItem('menuTree')
    }
  }
}) 